//
//  LMSNewLearnCenterUnJoinViewController.m
//  LMS
//
//  Created by dfws on 2019/3/9.
//  Copyright © 2019年 dfws. All rights reserved.
//

#import "LMSNewLearnCenterUnJoinViewController.h"
#import "Header.h"
#import "tableHeaderView.h"
#import "BsBottomWithColletionTableViewCell.h"
#import "BsColletionTableViewCell.h"
#import "BstTableContentViewCell.h"
#import "AuthorInfoTableViewCell.h"
#import "LMSNewXianzhiClassDetailModel.h"
#import "LMSNewIntroduceTableViewCell.h"
#import "BsGrayBorderColletionTableViewCell.h"
#import "DemoHelp.h"
#import <MJExtension/MJExtension.h>

static NSString *const IDE1 = @"BsBottomWithColletionTableViewCell__ide";


static NSString *const IDE3 = @"BstTableContentViewCell__ide";

static NSString *const IDE4 = @"AuthorInfoTableViewCell__ide";
static NSString *const IDIntroduce = @"LMSNewIntroduceTableViewCell__ide";
static NSString *const IDGB = @"BsGrayBorderColletionTableViewCell";
@interface LMSNewLearnCenterUnJoinViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic)LMSNewXianzhiClassDetailModel *detailModel;
@property(nonatomic)NSArray *lectureModelMarray;
@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *dataSource;

@property (nonatomic,strong) tableHeaderView *headView;

@property (nonatomic,strong) NSMutableArray *cellHeigthRowSArray;

@property (nonatomic,strong) UIButton *regisBtn;
@property(nonatomic)NSMutableDictionary *sectionMDic;
@end

@implementation LMSNewLearnCenterUnJoinViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self dealWithSectionNum];
    [self configUI];
    
    
}
- (void)loadData
{
    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"study" ofType:@"json"];
    NSData * data = [NSData dataWithContentsOfFile:filePath];
    NSDictionary * dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    
    self.detailModel = [LMSNewXianzhiClassDetailModel mj_objectWithKeyValues:dataDic[@"data"]];
    
    self.lectureModelMarray = [LMSNewXianzhiClasslectureModel mj_objectArrayWithKeyValuesArray:dataDic[@"data"][@"lecture"]];
}
- (void)dealWithSectionNum
{
    if (self.detailModel.knowledge_class_name.count) {
        [self.sectionMDic setObject:self.detailModel.knowledge_class_name forKey:@"knowledge_class_name"];
    }
    if (self.detailModel.compulsory_post_name.count) {
        [self.sectionMDic setObject:self.detailModel.compulsory_post_name forKey:@"compulsory_post_name"];
    }
    if (self.detailModel.optional_post_name.count) {
        [self.sectionMDic setObject:self.detailModel.optional_post_name forKey:@"optional_post_name"];
    }
    if (self.detailModel.course_description.length) {
        [self.sectionMDic setObject:self.detailModel.course_description forKey:@"course_description"];
    }
    if (  self.lectureModelMarray.count ) {
        [self.sectionMDic setObject: self.lectureModelMarray  forKey:@"lectureModelMarray"];
    }
    if (  self.detailModel.teacherModel.teacher_description ) {
        [self.sectionMDic setObject: @"1"  forKey:@"teacherModel"];
    }
    for (int i = 0; i <( self.sectionMDic.allKeys.count ); i++) {
        [self.cellHeigthRowSArray addObject:@30];
    }
}
- (void)configUI{
    [self.view addSubview:self.tableView];
    
    UIView *headContentView = [UIView new];
    [headContentView addSubview:self.headView];
    self.headView.course_title = self.detailModel.course_title;
    self.headView.course_description = [NSString stringWithFormat:@"%@\n\n课程%@门 学分%@分 时长 %@",self.detailModel.sub_course_title,self.detailModel.section_num,self.detailModel.course_credit,self.detailModel.course_length];
    self.headView.pictr = self.detailModel.course_picture;
    //要处理60秒转分的时间长度的问题
    [self.headView configureHeaderViewArray:self.detailModel.attribute];
    [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    headContentView.frame = CGRectMake(0, 0, kUIScreenWidth, [DemoHelp getViewHeight:_headView]);
    _tableView.tableHeaderView = headContentView;
    
    
    UIView *footerView = [UIView new];
    footerView.frame = CGRectMake(0, 0, kUIScreenWidth, 80);
    [footerView addSubview:self.regisBtn];
    [_regisBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(footerView);
        make.size.mas_equalTo(CGSizeMake(kUIScreenWidth-30, 35));
    }];
    _tableView.tableFooterView = footerView;
}
//点击变大缩小
- (void)cellClickAction:(NSIndexPath *)indexPath andCell:(BaseTableViewCell*)cell {
    if (![_cellHeigthRowSArray[indexPath.section] isEqualToNumber:@([cell realHeight])]) {
//        [self.tableView beginUpdates];
        _cellHeigthRowSArray[indexPath.section] = @([cell realHeight]);
//        [self.tableView endUpdates];
        [self.tableView reloadData];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4
    ;
    return (self.cellHeigthRowSArray.count);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [_cellHeigthRowSArray[indexPath.section] floatValue];
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    __weak LMSNewLearnCenterUnJoinViewController *weakVc = self;
    __weak  BaseTableViewCell *tmpcell = nil;
    if ([[self.sectionMDic allKeys][indexPath.section] isEqualToString:@"knowledge_class_name"] ) {
        BsBottomWithColletionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDE1 forIndexPath:indexPath];
        tmpcell = cell;
        cell.titile = @"知识点";
        [cell configureUIWithArray:self.detailModel.knowledge_class_name];
        cell.clickBlock = ^{
            [weakVc cellClickAction:indexPath andCell:tmpcell];
        };
        cell.refreshBlock = ^{
            [weakVc cellClickAction:indexPath andCell:tmpcell];
        };
        return cell;
    }
    else if ([[self.sectionMDic allKeys][indexPath.section] isEqualToString:@"course_description"]){
        
        LMSNewIntroduceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDIntroduce forIndexPath:indexPath];
        tmpcell = cell;
        cell.titile = @"课程简介";
        cell.backgroundColor = [UIColor whiteColor];
        [cell configureCellWithStr:self.detailModel.course_description];
        cell.refreshBlock = ^{
            [weakVc cellClickAction:indexPath andCell:tmpcell];
        };
        return cell;
    }else if ([[self.sectionMDic allKeys][indexPath.section] isEqualToString:@"compulsory_post_name"]){
        
        BsGrayBorderColletionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDGB forIndexPath:indexPath];
        tmpcell = cell;
        cell.titile = @"必修岗位";
        cell.backgroundColor = [UIColor whiteColor];
        [cell configureCellWithArray:self.detailModel.compulsory_post_name];//todowh
        cell.refreshBlock = ^{
            [weakVc cellClickAction:indexPath andCell:tmpcell];
        };
        return cell;
    }
    else if ([[self.sectionMDic allKeys][indexPath.section] isEqualToString:@"optional_post_name"]){
        
        BsGrayBorderColletionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDGB forIndexPath:indexPath];
        tmpcell = cell;
        cell.titile = @"选修岗位";
        cell.backgroundColor = [UIColor whiteColor];
        [cell configureCellWithArray:self.detailModel.optional_post_name];//todowh
        cell.refreshBlock = ^{
            [weakVc cellClickAction:indexPath andCell:tmpcell];
        };
        return cell;
    }
    
    else if ([[self.sectionMDic allKeys][indexPath.section] isEqualToString:@"lectureModelMarray"]){
        BstTableContentViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDE3 forIndexPath:indexPath];
        tmpcell = cell;
        cell.titile = @"课程目录";
        [cell configureUIWithArray:[self.lectureModelMarray mutableCopy]];
        cell.backgroundColor = [UIColor whiteColor];
        cell.refreshBlock = ^{
            [weakVc cellClickAction:indexPath andCell:tmpcell];
        };
        
        return cell;
    }
    else {
        AuthorInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDE4 forIndexPath:indexPath];
        tmpcell = cell;
        cell.titile = @"讲师介绍";
        cell.backgroundColor = [UIColor whiteColor];
        [cell configureCellWithStr:self.detailModel.teacherModel.teacher_description];
        cell.refreshBlock = ^{
            [weakVc cellClickAction:indexPath andCell:tmpcell];
        };
        cell.avator.backgroundColor = [UIColor purpleColor];
        return cell;
    }
    
    
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[BsBottomWithColletionTableViewCell class] forCellReuseIdentifier:IDE1];
        [_tableView registerClass:[BsGrayBorderColletionTableViewCell class] forCellReuseIdentifier:IDGB];
        [_tableView registerClass:[BstTableContentViewCell class] forCellReuseIdentifier:IDE3];
        [_tableView registerClass:[AuthorInfoTableViewCell class] forCellReuseIdentifier:IDE4];
        [_tableView registerClass:[LMSNewIntroduceTableViewCell class] forCellReuseIdentifier:IDIntroduce];
    }
    return _tableView;
}
- (tableHeaderView *)headView{
    if (!_headView) {
        _headView  = [[tableHeaderView alloc]init];
    }
    return _headView;
}
//- (UIButton *)regisBtn{
//    if (!_regisBtn) {
//        _regisBtn = [UIButton new];
//        [_regisBtn setTitle:@"申请学习" forState:UIControlStateNormal];
//        _regisBtn.backgroundColor = kUIColorFromRGB(0x0085FE);
//        _regisBtn.clipsToBounds = YES;
//        _regisBtn.layer.cornerRadius = 4;
//        _regisBtn.height = 40;
//        _regisBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
//    }
//    return _regisBtn;
//}


- (NSMutableDictionary *)sectionMDic
{
    if (!_sectionMDic) {
        self.sectionMDic = [NSMutableDictionary dictionary];
    }
    return _sectionMDic;
}
- (NSMutableArray *)cellHeigthRowSArray
{
    if (!_cellHeigthRowSArray) {
        self.cellHeigthRowSArray = [NSMutableArray array];
    }
    return _cellHeigthRowSArray;
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


@end
